﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

using RandomDemo.Models;
using RandomDemo.TokenFiles;
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;


namespace RandomDemo.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class SysUserController : ControllerBase
    {
        private Microsoft.EntityFrameworkCore.DbContext _db;

        public JwtPayload TokenResult { get; set; }
        public SysUserController(Microsoft.EntityFrameworkCore.DbContext db)
        {
            _db=db;
        }

        [HttpPost("Login")]
        public ResultClass Login(SysUser user)
        {
            var model = _db.Set<SysUser>().Where(m=>m.Account==user.Account&&m.PassWord==user.PassWord).FirstOrDefault();
            if (model != null)
            {
                JwtPayload keyValuePairs = new JwtPayload();
                keyValuePairs.Add("UserId", model.UserId);
                keyValuePairs.Add("Account", model.Account);
                keyValuePairs.Add("RoleId", model.RoleId);
                return new ResultClass() { Code = 200, Message = "登录成功！", Result = Token.CreateToken(keyValuePairs) };
            }
            return new ResultClass() { Code=200,Message="账号或者密码错误",Result="" };
        }

        /// <summary>
        /// 根据Token得到用户信息
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetUserByToken")]
        public async Task<ResultClass> GetUserByToken()
        {
            var temp = TokenResult.Where(m => m.Key.Equals("UserId")).FirstOrDefault();
            if (temp.Key != null)
            {
                var model = _db.Set<SysUser>().Where(m => m.UserId.ToString().Equals(temp.Value)).FirstOrDefault();
                //只能看指定内容，某些东西不允许发送给前台
                return await Task.FromResult(new ResultClass() { Code = 200, Message = "操作成功", Result =new { UserId=model.UserId,Account=model.Account, Records = model.Records } });
            }
            return await Task.FromResult<ResultClass>(new ResultClass() { Code = 200, Message = "操作失败", Result = "" });
        }

        /// <summary>
        /// 更新Token
        /// </summary>
        /// <returns></returns>
        [HttpGet("RefreshToken")]
        public async Task<ResultClass> RefreshToken()
        {
            var temp = TokenResult.Where(m => m.Key.Equals("UserId")).FirstOrDefault();
            if (temp.Key != null)
            {
                var model = _db.Set<SysUser>().Where(m => m.UserId.ToString().Equals(temp.Value)).FirstOrDefault();
                if (model != null)
                {
                    JwtPayload keyValuePairs = new JwtPayload();
                    keyValuePairs.Add("UserId", model.UserId);
                    keyValuePairs.Add("Account", model.Account);
                    keyValuePairs.Add("RoleId", model.RoleId);
                    return await Task.FromResult(new ResultClass() { Code = 200, Message = "操作成功", Result = Token.CreateToken(keyValuePairs) });
                }
            }
            return await Task.FromResult<ResultClass>(new ResultClass() { Code = 200, Message = "操作失败", Result = "" });
        }

        [HttpGet("Logon")]
        public async Task<ResultClass> Logon()
        {
            //Token.RefreshToken(-10000);
            return await Task.FromResult(new ResultClass() { Code = 200, Message = "操作成功", Result = "" });
        }


        [HttpGet("Test")]
        [TokenFilter]
        public async Task<ResultClass> Test()
        {
            await Task.Delay(5000);
            var temp= TokenResult.Where(m => m.Key.Equals("UserId")).FirstOrDefault();
            if (temp.Key != null)
            {
                var model = _db.Set<SysUser>().Where(m => m.UserId.ToString().Equals(temp.Value));
                return await Task.FromResult(new ResultClass() { Code = 200, Message = "账号或者密码错误", Result = model });
            }
            return await Task.FromResult< ResultClass > (new ResultClass() { Code = 200, Message = "账号或者密码错误", Result = "" });
        }
        [HttpGet("Test1")]
        [TokenFilter]
        public async Task<ResultClass> Test1()
        {
            var temp = TokenResult.Where(m => m.Key.Equals("UserId")).FirstOrDefault();
            if (temp.Key != null)
            {
                var model = _db.Set<SysUser>().Where(m => m.UserId.ToString().Equals(temp.Value));
                return await Task.FromResult(new ResultClass() { Code = 200, Message = "账号或者密码错误", Result = model });
            }
            return await Task.FromResult<ResultClass>(new ResultClass() { Code = 200, Message = "账号或者密码错误", Result = "" });
        }
    }
}
